package com.example.hotelapp.Fragment;

import android.app.Activity;
import android.os.Handler;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotelapp.Adapter.HomeAdapter;
import com.example.hotelapp.Adapter.OrderAdapter;
import com.example.hotelapp.Base.BaseFragment;
import com.example.hotelapp.Bean.BookInfoBean;
import com.example.hotelapp.Bean.HomeIndexBean;
import com.example.hotelapp.Bean.OrderTypeBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.DYLoadingView;
import com.example.utils.CommonUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.kongzue.dialog.v2.WaitDialog;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.*;
import static com.example.utils.CommonUtils.*;

/**
 * 订单页
 */
public class OrderFragment extends BaseFragment {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ld)
    DYLoadingView ld;
    private Activity activity;
    private OrderAdapter orderAdapter;

    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_order;
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = getActivity();
        String uid = SharedInfo(activity,"GetUid","id");
        initView();
        GetData(uid,"");
    }

    private void GetData(String member_id,String states) {
        ld.start();
        ld.setVisibility(View.VISIBLE);
        OKPostValue("memberId", member_id,"states",states,AppNetConfig.SelectbookinfoView, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ld.stop();
                        ld.setVisibility(View.GONE);
                        ToastShow(activity,"服务器出错，请稍后再试");
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取返回的json数据并解析
                String json = response.body().string();
                System.out.println(json);
                BookInfoBean databean = new Gson().fromJson(json, BookInfoBean.class);
                int code = databean.getCode();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ld.stop();
                        ld.setVisibility(View.GONE);
                        if (code == 1) {
                            //请求成功
                            recyclerView.setLayoutManager(new GridLayoutManager(activity, 1));
                            orderAdapter = new OrderAdapter(activity, databean.getData());
                            recyclerView.setAdapter(orderAdapter);
                            orderAdapter.setItemClickListener(new OrderAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(RecyclerView.ViewHolder viewHolder, int id, int position) {
                                    WaitDialog.show(activity,"支付中...");
                                    OKPostValue("id", String.valueOf(id), "states", "已支付", AppNetConfig.UpdatePay, new Callback() {
                                        @Override
                                        public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                        }

                                        @Override
                                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                                        }
                                    });

                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            String uid = SharedInfo(activity,"GetUid","id");
                                            WaitDialog.dismiss();
                                            GetData(uid,"");
                                            DialogShow(activity,"支付成功");
                                        }
                                    },3000);
                                }
                            });
                        } else {
                            ToastShow(activity,"无数据");
                            recyclerView.setAdapter(null);
                        }
                    }
                });
            }
        });
    }

    /**
     * 加载数据
     */
    private void initView() {
        OrderTypeBean databean = new Gson().fromJson(AppNetConfig.OrderTypeJson, OrderTypeBean.class);
        for (int i = 0; i < 5; i++) {
            tabs.addTab(tabs.newTab().setText(databean.getData().get(i).getType_name()));
        }

        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String uid = SharedInfo(activity,"GetUid","id");
                if (tabs.getSelectedTabPosition() == 0){
                    GetData(uid,"");
                }else if (tabs.getSelectedTabPosition() == 1){
                    GetData(uid,"待支付");
                }else if (tabs.getSelectedTabPosition() == 2){
                    GetData(uid,"已支付");
                }else if (tabs.getSelectedTabPosition() == 3){
                    GetData(uid,"已入住");
                }else if (tabs.getSelectedTabPosition() == 4){
                    GetData(uid,"已取消");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
